import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/page/train/train_user_check_recoder_other_remark_add_page.dart';
import 'package:get/get.dart';

import '../../bean/bean_train.dart';
import '../../utils/toast_utils.dart';
import '../common/common_loading_dialog.dart';
import '../common/common_nodata_widget.dart';
import 'controller/train_other_user_check_recoder_controller.dart';

///教师的培训
class TrainOtherUserCheckRecoderPage extends StatefulWidget {
  final String tag;

  final int trainId;
  final int userId;

  const TrainOtherUserCheckRecoderPage(
      {Key key,
      @required this.trainId,
      @required this.userId,
      @required this.tag})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _TrainOtherUserCheckRecoderPageState();
  }
}

class _TrainOtherUserCheckRecoderPageState
    extends State<TrainOtherUserCheckRecoderPage>
    with SingleTickerProviderStateMixin {
  TrainOtherUserCheckRecoderController _checkRecoderController;

  @override
  void initState() {
    super.initState();
    _checkRecoderController = Get.put(
      TrainOtherUserCheckRecoderController(widget.trainId, widget.userId),
      tag: widget.tag,
    );

    Future.delayed(Duration.zero, () async {
      await _checkRecoderController.requestUserCheckRecoderFunction();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("考核详情"),
      ),
      body: GetBuilder(
        id: "root",
        tag: widget.tag,
        builder: (TrainOtherUserCheckRecoderController controller) {
          return buildAnimatedSwitcher(context);
        },
      ),
    );
  }

  AnimatedSwitcher buildAnimatedSwitcher(BuildContext context) {
    return AnimatedSwitcher(

        //动画执行切换时间
        duration: const Duration(milliseconds: 200),
        //动画构建器 构建指定动画类型
        transitionBuilder: (Widget child, Animation<double> animation) {
          //执行缩放动画
          return FadeTransition(child: child, opacity: animation);
        },
        //执行动画的子 Widget
        //只有子 Widget 被切换时才会触发动画
        child: buildMyScoreHisIntegralWidget());
  }

  buildMyScoreHisIntegralWidget() {
    bool _isLoading = _checkRecoderController.getIsLoading();
    if (_isLoading) {
      return Center(
        child: LoadingWidget(),
      );
    }
    TrainUserCheckRecoderEntity _checkRecoderEntity =
        _checkRecoderController.getTrainUserCheckRecoderEntity();

    if (_checkRecoderEntity == null) {
      return Center(
        child: NoDataWidget(
          text: "暂无考核结果",
          clickCallBack: () {
            _checkRecoderController.requestUserCheckRecoderFunction(
                click: true);
          },
        ),
      );
    }

    return RefreshIndicator(
      onRefresh: () async {
        await _checkRecoderController.requestUserCheckRecoderFunction();
        ToastUtils.showToast("已刷新");
        return true;
      },
      child: SingleChildScrollView(
        child: buildListView(),
      ),
    );
  }

  buildListView() {
    TrainUserCheckRecoderEntity _checkRecoderEntity =
        _checkRecoderController.getTrainUserCheckRecoderEntity();

    int showUpdateMyRemark = _checkRecoderEntity.showUpdateMyRemark;
    int statues = _checkRecoderEntity.statues;
    Color color = Colors.grey;
    String statuesStr = "未考评";
    if (statues == 1) {
      color = Colors.green;
      statuesStr = "已考评";
    }
    return Column(
      children: [
        buildUserInfo(_checkRecoderEntity),
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    child: Text(
                      "请注意 培训结束后，不可编辑考证项，请在培训结束前考评完成，考证完成后，才可查看用户的考核表",
                      style: TextStyle(
                        fontSize: 12,
                        color: Colors.red,
                      ),
                    ),
                    margin: EdgeInsets.only(bottom: 8),
                  ),
                  Row(
                    children: [
                      Text("考评项"),
                      SizedBox(
                        width: 20,
                      ),
                      Text(
                        statuesStr,
                        style: TextStyle(color: color),
                      ),
                      Expanded(child: SizedBox()),
                      showUpdateMyRemark == 0
                          ? TextButton(
                              onPressed: () async {
                                await Get.to(
                                  () => TrainOtherUserCheckRecoderAddPage(
                                    trainUserCheckRecoderEntity:
                                        _checkRecoderEntity,
                                  ),
                                );
                                _checkRecoderController
                                    .requestUserCheckRecoderFunction(
                                        click: true);
                              },
                              child: Text("编辑"))
                          : SizedBox()
                    ],
                  ),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "出勤情况:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      SizedBox(width: 10),
                      Text("${_checkRecoderEntity.attendanceNumber}分"),
                    ],
                  ),
                  SizedBox(height: 4),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "纪律执行:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      SizedBox(width: 10),
                      Text("${_checkRecoderEntity.disciplineNumber}分"),
                    ],
                  ),
                  SizedBox(height: 4),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "学习表现:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      SizedBox(width: 10),
                      Text("${_checkRecoderEntity.studyNumber}分"),
                    ],
                  ),
                  SizedBox(height: 4),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "实训表现:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      SizedBox(width: 10),
                      Text("${_checkRecoderEntity.practicalNumber}分"),
                    ],
                  ),
                  SizedBox(height: 4),
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "知识掌握:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      SizedBox(width: 10),
                      Text("${_checkRecoderEntity.knowledgeNumber}分"),
                    ],
                  ),
                ],
              )),
        ),
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "学员自我鉴定:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      Expanded(
                        child: SizedBox(width: 10),
                      ),
                    ],
                  ),
                  SizedBox(height: 4),
                  Text("${_checkRecoderEntity.myRemark ?? "无"}")
                ],
              )),
        ),
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "班主任鉴定:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      Expanded(
                        child: SizedBox(width: 10),
                      ),
                    ],
                  ),
                  SizedBox(height: 4),
                  _checkRecoderEntity.directorRemark == null
                      ? Text("无")
                      : Text("${_checkRecoderEntity.directorRemark}")
                ],
              )),
        ),
        Container(
          color: Colors.white,
          width: double.infinity,
          margin: EdgeInsets.only(bottom: 12),
          child: Padding(
              padding: EdgeInsets.all(10),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisSize: MainAxisSize.min,
                children: [
                  Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Text(
                        "培训机构意见:",
                        style: TextStyle(
                            fontWeight: FontWeight.w500, color: Colors.black),
                      ),
                      Expanded(
                        child: SizedBox(width: 10),
                      ),
                    ],
                  ),
                  SizedBox(height: 4),
                  _checkRecoderEntity.trainRemark == null
                      ? Text("无")
                      : Text("${_checkRecoderEntity.trainRemark}")
                ],
              )),
        ),
      ],
    );
  }

  buildUserInfo(TrainUserCheckRecoderEntity checkRecoderEntity) {
    UserBean userBean = checkRecoderEntity.userBean;
    if (userBean == null) {
      return SizedBox();
    }
    return Container(
      color: Colors.white,
      width: double.infinity,
      padding: EdgeInsets.all(12),
      margin: EdgeInsets.only(bottom: 12),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Expanded(
                child: Text(
                  "姓名:${userBean.reamName}",
                  style: TextStyle(
                      fontWeight: FontWeight.w500, color: Colors.black),
                ),
              ),
              Expanded(
                child: Text(
                  "电话:${userBean.mobile}",
                  style: TextStyle(
                      fontWeight: FontWeight.w500, color: Colors.black),
                ),
              ),
            ],
          ),
          Text(
            "部门:${userBean.department}",
            style: TextStyle(fontWeight: FontWeight.w500, color: Colors.black),
          ),
          Text(
            "身份证号:${userBean.idCard}",
            style: TextStyle(fontWeight: FontWeight.w500, color: Colors.black),
          )
        ],
      ),
    );
  }
}
